package com.luo.chengrui.module.sa.api.auth;

import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import com.luo.chengrui.module.sa.dal.dataobject.role.PermissionViewDO;
import com.luo.chengrui.module.sa.service.org.OrgService;
import com.luo.chengrui.module.sa.service.role.PermissionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class SaAuthApiImpl implements SaAuthApi {
    @Resource
    private PermissionService permissionService;
    @Resource
    private OrgService orgService;

    @Override
    public Set<Long> getMenuIds(Long systemUserId) {
        List<OrgDO> orgDOList = orgService.getPsmListBySystemUserId(systemUserId);
        List<PermissionViewDO> allMenuLsit = new ArrayList<>();
        for (OrgDO orgDO : orgDOList) {
            List<PermissionViewDO> menuLsit = permissionService.getMenuListByOrgFullId(orgDO.getFullId());
            allMenuLsit.addAll(menuLsit);
        }
        Set<Long> menuIds = allMenuLsit.stream().map(PermissionViewDO::getMenuId).collect(Collectors.toSet());
        return menuIds;
    }
}
